﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

/// <summary>
/// rank 的摘要说明
/// </summary>
public class rank_t
{

    public static string constr =getsql.getstr("activestr");
    public static string levelcon = getsql.getstr("levelstr");
  //  public static string constr = System.Configuration.ConfigurationSettings.AppSettings["activestr"].ToString();
   // public static string levelcon = System.Configuration.ConfigurationSettings.AppSettings["levelstr"].ToString();
    protected string dastr = DateTime.Now.DayOfWeek.ToString();
    protected string dahour = DateTime.Now.Hour.ToString();
    public rank_t()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    //判断是否领奖
    public bool IsGetReward(int useridx, int gid, int type)
    {
        int state = 0;
        SqlParameter[] pars ={
                       SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                       SqlHelper.MakeInParam("@gid",SqlDbType.Int,4,gid),
                       SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                       SqlHelper.MakeOutParam("@state",SqlDbType.Int,4),
            SqlHelper.MakeInParam("@multitype",SqlDbType.Int,4,58)
        };
        SqlHelper.ExecuteNonQuery(constr, CommandType.StoredProcedure, "IsGetReward", pars);
        state = Convert.ToInt32(pars[3].Value);
        if (state == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public string GetOtherRank(int type, int giftid)
    {
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@type", SqlDbType.Int, 4, type), SqlHelper.MakeInParam("@giftid", SqlDbType.Int, 4, giftid) };
        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "Get_Other_Histry", pars);
        string str = "";
        if (dat != null)
        {
            StringBuilder strbuil = new StringBuilder();
            int len = dat.Rows.Count;
            for (int k = 0; k < len; k++)
            {
                string cla = string.Empty;
                if (k == 0)
                {
                    cla = " fc01";
                }
                strbuil.AppendFormat("<li><span class='l_w1{4}'>{0}</span><span class='l_w2'><a title='{1}'>{2}</a></span><span class='l_w3'>{3}</span></li>", k + 1, dat.Rows[k]["useridx"], dat.Rows[k]["myname"], dat.Rows[k]["num"], cla);
            }

            str = strbuil.ToString();
        }
        return str;
    }

    //得到历史排名
    public string GetHistoryRank(int giftid, int type)
    {
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@giftid", SqlDbType.Int, 4, giftid) };
        DataTable dat = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "Get_History_Rank", pars);
        string str = "";
        if (dat != null)
        {
            StringBuilder strbuil = new StringBuilder();
            int len = dat.Rows.Count;
            for (int k = 0; k < len; k++)
            {
                if (type == 1)
                {
                    string cla = string.Empty;
                    if (k == 0)
                    {
                        cla = " fc01";
                    }
                    strbuil.AppendFormat("<li><span class='l_w1{4}'>{0}</span><span class='l_w2'><a title='{1}'>{2}</a></span><span class='l_w3'>{3}</span></li>", k + 1, dat.Rows[k]["useridx"], dat.Rows[k]["myname"], dat.Rows[k]["num"], cla);
                }
                if (type == 2)
                {
                    string kinstr = "";
                    int knum = pccheck(dat.Rows[k]["useridx"].ToString(), giftid);
                    for (int s = 0; s < knum; s++)
                    {
                        kinstr += "<span class='jb1'></span>";
                    }
                    if (knum == 3)
                    {
                        kinstr = "<span class='jb2'></span>";
                    }
                    strbuil.AppendFormat("<li><span class='sbt_num'>{0}</span><span class='sbt_na'><a title='{1}'>{2}</a></span><span><span>{3}次</span></span></li>", k + 1, dat.Rows[k]["useridx"], dat.Rows[k]["myname"], dat.Rows[k]["num"]);
                }
            }
            str = strbuil.ToString();
        }
        return str;
    }

    //是否有青蛙图标
    public string levelsearch(int useridx, int sex)
    {

        DataTable metable = null;
        string stagepic = "";
        SqlParameter[] p =
					{
						SqlHelper.MakeInParam("@useridx", SqlDbType.Int,4, useridx),
					};
        metable = SqlHelper.ExecuteDataTable(levelcon, CommandType.StoredProcedure, "f_userleve_search", p);
        if (metable.Rows.Count > 0)
        {
            int stage = int.Parse(metable.Rows[0]["userrank"].ToString());
            int stagevalue = int.Parse(metable.Rows[0]["curexp"].ToString());
            int nexvalue = int.Parse(metable.Rows[0]["nextexp"].ToString());
            if (stage <= 19)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa11.gif'' align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl1.gif'  align='absmiddle' />";
            }
            if (stage > 19 && stage <= 39)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa22.gif'    align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl2.gif'   align='absmiddle' />";

            }
            if (stage > 39 && stage <= 59)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa33.gif'   align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl3.gif'  width='20px' height='21px'  align='absmiddle' />";

            }
            if (stage > 59 && stage <= 79)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa44.gif'   align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl4.gif'    align='absmiddle' />";

            }
            if (stage > 79 && stage <= 99)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa55.gif'   align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl5.gif'    align='absmiddle' />";

            }
            if (stage == 100)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa66.gif'    align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl6.gif'   align='absmiddle' />";

            }
            string titlestr = "等级：" + stage + ";经验：" + stagevalue + "/" + nexvalue;
            stagepic = "<a title='" + titlestr + "'>" + stagepic + "</a>";
        }
        return stagepic;
    }

    //得到连冠杯次数
    public int Concheck(string useridx, int giftid)
    {
        int count = 0;
        int sendidx = Convert.ToInt32(useridx);
        string sql = "exec f_pro_continuchek " + sendidx + "," + giftid;
        try
        {
            //				DataSet ds=getds(sql);
            //				DataTable dt=ds.Tables[0];
            //				count=int.Parse(dt.Rows[0][0].ToString());
            SqlParameter[] p =
					{
						SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,int.Parse(useridx)),
						SqlHelper.MakeInParam("@giftid",SqlDbType.Int,4,giftid),
				//		SqlHelper.MakeInParam("@multitype",SqlDbType.Int,4,1)
						//					@useridx int,                          ------用户编号
						//@giftid  int,							------道具编号
						//@multitype int
					};
            DataTable metable = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "dance_pro_continuchek", p);
            count = int.Parse(metable.Rows[0][0].ToString());
        }
        catch
        {
        }

        return count;
    }

    //得到所有礼物标签
    public DataSet getpds(int gift, int type)
    {
        SqlParameter[] p =
				{
					SqlHelper.MakeInParam("@giftid",SqlDbType.Int,4,gift),
					SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type)
				};
        DataSet pds = SqlHelper.ExecuteDataset(constr, CommandType.StoredProcedure, "dance_propect_search", p);
        return pds;
    }

    //得到道具及数据  
    /// <summary>
    /// 得到道具及数据  
    /// </summary>
    /// <param name="giftid">礼物id</param>
    /// <param name="type">上周或本周1为上周，2为本周</param>
    /// <returns></returns>
    public string getstar(int giftid, int type)
    {
        string useridx = "";
        string userid = "";
        string username = "";
        string gnum = "";
        int knum = 0;
        string restr = "<li></li>";
        string kinstr = "";
        DataSet ds = getpds(giftid, type);
        if (ds.Tables.Count > 0)
        {
            StringBuilder strbuil = new StringBuilder();
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow[] dr = dt.Select("", "outquantity desc");

                if (type == 1)
                {
                    useridx = dr[0]["useridx"].ToString();
                    userid = dr[0]["id"].ToString();
                    username = dr[0]["myname"].ToString();
                    gnum = dr[0]["outquantity"].ToString();
                  //  if (dahour == "23")
                  //      gnum = "";
                    knum = Concheck(useridx, giftid);
                    string expnum = "";
                    for (int s = 0; s < knum; s++)
                    {
                        kinstr += "<span class='jb1'></span>";
                        if (knum == 2)
                            expnum = "1600";
                        if (knum == 1)
                            expnum = "800";
                    }
                    if (knum == 3)
                    {
                        kinstr = "<span class='jb2'></span>";
                        expnum = "6000";
                    }
                    if (knum == 1)
                    {
                        expnum = "800";
                    }
                    if (knum == 2)
                    {
                        expnum = "1600";
                    }
                    if (knum == 3)
                    {
                        if (giftid == 35 || giftid == 36 || giftid == 102)
                            expnum = "6000";
                        else
                        {
                            if (giftid == 81 || giftid == 27 || giftid == 10 || giftid == 14 || giftid == 9 || giftid == 8 || giftid == 16 || giftid == 11)
                                expnum = "3000";
                            else
                                expnum = "2500";
                        }
                    }
                    string str = "";
                    if(IsGetReward(Convert.ToInt32(useridx),giftid,2))
                    {
                      str="<li><span class='s_qr'></span></li>";
                    }
                    strbuil.AppendFormat("<li><span>昵称：<a title='{0}'>{1}</a></span></li><li><span>数量：{2}次</span>{3}</li><li><span>{4}</span><span class='s_jy'>" + expnum + "点</span></li>{5}", useridx, username, gnum, kinstr, levelsearch(Convert.ToInt32(useridx), 1),str);
                }
                if (type == 2)
                {
                    int rowlength = 3;
                    if (dr.Length < 3)
                        rowlength = dr.Length;
                    for (int i = 0; i < rowlength; i++)
                    {
                        kinstr = "";
                        useridx = dr[i]["useridx"].ToString();
                        userid = dr[i]["id"].ToString();
                        username = dr[i]["myname"].ToString();
                        gnum = dr[i]["outquantity"].ToString()+"次";
                    int min = DateTime.Now.Minute;
                   if (dastr == "Sunday" && dahour == "23" )
                        gnum = "";
                        knum = Concheck(useridx, giftid);
                        for (int s = 0; s < knum; s++)
                        {
                            kinstr += "<span class='jb1'></span>";
                        }
                        if (knum == 3)
                        {
                            kinstr = "<span class='jb2'></span>";
                        }
                        strbuil.AppendFormat("<li><span class='sbt_num'>{0}</span><span class='sbt_na'><a title='{1}'>{2}</a></span><span class='sbt_va'><span>{3}</span>{4}</span></li>", i + 1, useridx, username, gnum, kinstr);
                    }
                }
                restr = strbuil.ToString();
            }
            if (giftid == 33 || giftid == 18)
                restr = "<li></li>";
        }
        if(restr.Length<=1)
        {
            restr = "<li></li><li></li><li></li>";
        }
        return restr;
    }

    //拼接印章和鉴定
    public string getpr(int giftid, int type)
    {
        string useridx = "";
        string userid = "";
        string username = "";
        string gnum = "";
        int knum = 0;
        string restr = "";
        string kinstr = "";
        DataSet ds = getpds(giftid, type);
        if (ds.Tables.Count > 0)
        {
            StringBuilder strbuil = new StringBuilder();
            if (type == 3 || type == 5)
                giftid = 0;
            else
                giftid = 999;
            DataTable dt = ds.Tables[0];
            DataRow[] dr = dt.Select();
            if (type == 3 || type == 4)
            {
                useridx = dr[0]["useridx"].ToString();
                userid = dr[0]["id"].ToString();
                username = dr[0]["myname"].ToString();
                if (type == 4)
                    gnum = dr[0]["gcash"].ToString();
                else
                    gnum = dr[0]["gnum"].ToString();
              //  if (dahour == "23")
               //     gnum = "";
                knum = pccheck(useridx, giftid);
                for (int s = 0; s < knum; s++)
                {
                    kinstr += "<span class='jb1'></span>";
                }
                if (knum == 3)
                {
                    kinstr = "<span class='jb2'></span>";
                }
                strbuil.AppendFormat("<li><span>昵称：<a title='{0}'>{1}</a></span></li><li><span>数量：{2}次</span>{3}</li><li><span>{4}</span><span class='s_jy'>5655点</span></li>", useridx, username, gnum, kinstr, levelsearch(Convert.ToInt32(useridx), 1));
            }
            if (type == 5 || type == 6)
            {
                int rowlength = 3;
                if (dr.Length < 3)
                    rowlength = dr.Length;
                for (int i = 0; i < rowlength; i++)
                {
                    kinstr = "";
                    useridx = dr[i]["useridx"].ToString();
                    userid = dr[i]["id"].ToString();
                    username = dr[i]["myname"].ToString();
                    gnum = dr[i]["gnum"].ToString()+"次";
                    if (dahour == "23")
                        gnum = "";
                    knum = pccheck(useridx, giftid);
                    for (int s = 0; s < knum; s++)
                    {
                        kinstr += "<span class='jb1'></span>";
                    }
                    if (knum == 3)
                    {
                        kinstr = "<span class='jb2'></span>";
                    }
                    strbuil.AppendFormat("<li><span class='sbt_num'>{0}</span><span class='sbt_na'><a title='{1}'>{2}</a></span><span class='sbt_va'><span>{3}</span>{4}</span></li>", i + 1, useridx, username, gnum, kinstr);

                }
            }
            restr = strbuil.ToString();
        }
        return restr;
    }

    //得到印章和鉴定的idx及type
    public int pccheck(string useridx, int giftid)
    {
        int count = 0;
        int sendidx = Convert.ToInt32(useridx);
        string sql = "exec dance_print_continuchek " + sendidx + "," + giftid;
        try
        {
            //				DataSet ds=getds(sql);
            //				DataTable dt=ds.Tables[0];
            //				count=int.Parse(dt.Rows[0][0].ToString());
            SqlParameter[] p =
					{
						SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,int.Parse(useridx)),
						SqlHelper.MakeInParam("@giftid",SqlDbType.Int,4,giftid)
						//					@useridx int,                          ------用户编号
						//@giftid  int,							------道具编号
						//@multitype int
					};
            DataTable metable = SqlHelper.ExecuteDataTable(constr, CommandType.StoredProcedure, "dance_print_continuchek", p);
            count = int.Parse(metable.Rows[0][0].ToString());
        }
        catch
        {
        }

        return count;

    }

    public string getconsume(int type)
    {
        string roomid = "";
        string roomname = "";
        string gnum = "";
        string restr = "";
        DataSet ds = getcds(type);
        if (ds.Tables.Count > 0)
        {
            DataTable dt = ds.Tables[0];
            DataRow[] dr = dt.Select();
            StringBuilder strbuil = new StringBuilder();
            for (int i = 0; i < dr.Length; i++)
            {
                string cla = "";
                if (i % 2 == 1)
                {
                    cla = "class='bcg02'";
                }
                if (type == 1)
                {
                    roomid = dr[i]["roomid"].ToString();
                    roomname = dr[i]["name"].ToString();
                    gnum = dr[i]["roomconsume"].ToString();
                    strbuil.AppendFormat("<tr {4}><td class='numbg'>{0}</td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid=" + roomid + "&type=1'>{1}<a></td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid=" + roomid + "&type=1'>{2}<a></td><td>{3}</td></tr>", i + 1, roomid, roomname, gnum, cla);

                }
                if (type == 2)
                {
                    roomid = dr[i]["roomid"].ToString();
                    roomname = dr[i]["name"].ToString();
                    gnum = dr[i]["avp"].ToString();
                    strbuil.AppendFormat("<tr {4}><td class='numbg'>{0}</td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid=" + roomid + "&type=1'>{1}<a></td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid=" + roomid + "&type=1'>{2}<a></td><td>{3}</td></tr>", i + 1, roomid, roomname, gnum, cla);
                }
                //if (type == 3)
                //{
                //    roomid = dr[i]["useridx"].ToString();
                //    roomname = dr[i]["myname"].ToString();
                //    gnum = dr[i]["outquantity"].ToString();
                //    restr += "<li><span class='b_num'>" + (i + 1) + "</span><span class='b_na'><a  title='" + roomid + "'>" + roomname + "</a></span>";
                //    restr += "<span class='b_va'>" + gnum + "</span></li>";
                //}

            }
            restr = strbuil.ToString();

        }
        return restr;
    }

    public DataSet getcds(int type)
    {
        string sql = "";
        DataSet meds = null;
        if (type == 1)
        {
            SqlParameter[] p ={ };
            sql = "dance_roomconsume_list";
            meds = SqlHelper.ExecuteDataset(constr, CommandType.StoredProcedure, sql, p);
        }
        if (type == 2)
        {
            string idate1 = DateTime.Now.ToString();
            sql = "dance_kroomavp";
            SqlParameter[] p ={ SqlHelper.MakeInParam("@idate", SqlDbType.DateTime, 20, idate1) };
            meds = SqlHelper.ExecuteDataset(constr, CommandType.StoredProcedure, sql, p);
        }
        //if (type == 3)
        //{
        //    SqlParameter[] p ={ };
        //    sql = "f_day_consumelist";
        //    meds = SqlHelper.ExecuteDataset(constr, CommandType.StoredProcedure, sql, p);
        //}
        return meds;
    }
}
